/**
 * Scales hulu's embedded player for full screen.
 */

var embedStorage = "embedChromeless";
var embedVideoId;

function playMedia()
{
	var dctk = connected.toolkit;
	
	videoId = movieUrl[cursorPosition+1].split("/");
	connected.debug.print(videoId[4]);
	
	var params = connected.createPageDescriptor();
	//params.uri = "http://www.hulu.com/player.swf?eid="+videoId[4]+"&user_id="+queueId+"&st=0";
	//params.uri = "http://www.hulu.com/player.swf?content_id="+contentId+"&user_id="+queueId+"&st=0";

	params.pageType = dcIConnected.PT_HTML;
	var height = connected.outputHeight;
 	var width = connected.outputWidth;
	if (width/height >= 16/9) {
		//params.uri = "chromeless.htm";
		params.uri = "http://www.hulu.com/site-player/playerembed.swf?referrer=none&eid="+embed+"&st=&et=&it=&siteHost=http://www.hulu.comhttp://www.hulu.com/embed/"+embed+"&popout=true&plugins=1&modes=0,4&initMode=4&sortBy=&continuous_play_on=false&inPlaylist=1&stage_width=512&stage_height=296&user_id="+queueId;
		params.viewConstraints.addResolutionConstraint(512, 285); //16:9
 		}
 	else if (width/height < 16/9) {
		//params.uri = "chromeless_4-3.htm";
		params.uri = "http://www.hulu.com/site-player/playerembed.swf?referrer=none&eid="+embed+"&st=&et=&it=&siteHost=http://www.hulu.comhttp://www.hulu.com/embed/"+embed+"&popout=true&plugins=1&modes=0,4&initMode=4&sortBy=&continuous_play_on=false&inPlaylist=1&stage_width=512&stage_height=296&user_id="+queueId;
		params.viewConstraints.addResolutionConstraint(380, 285); //4:3
 		}
	params.audioCapture = true;
	//params.htmlInputFocusMode = dcIPageDescriptor.FOCUS_MODE_OBJECT;
	// load the page
	var storage = connected.openStorage(embedStorage,true)
	storage.set(embedVideoId,embed) //contentId
	
	connected.debug.print("Playing: http://www.hulu.com/site-player/playerembed.swf?referrer=none&eid="+embed+"&st=&et=&it=&siteHost=http://www.hulu.com&user_id="+queueId);
	//connected.debug.print("Playing: http://www.hulu.com/player.swf?eid="+videoId[4]+"&user_id="+queueId+"&st=0");
	connected.loadFromPageDescriptor(params);	
}

function resumeMedia()
{
	var dctk = connected.toolkit;
	
	videoId = embedUrl[cursorPosition+1].split("/");
	connected.debug.print(videoId[4]);
	
	var params = connected.createPageDescriptor();
	//params.uri = "http://www.hulu.com/player.swf?eid="+videoId[4]+"&user_id="+queueId;
	//params.uri = "http://www.hulu.com/player.swf?content_id="+contentId+"&user_id="+queueId;
	
	params.pageType = dcIConnected.PT_HTML;
	var height = connected.outputHeight;
 	var width = connected.outputWidth;
	if (width/height >= 16/9) {
		//params.uri = "chromeless.htm";
		params.uri = "http://www.hulu.com/site-player/playerembed.swf?referrer=none&eid="+embed+"&st=&et=&it=&siteHost=http://www.hulu.comhttp://www.hulu.com/embed/"+embed+"&popout=true&plugins=1&modes=0,4&initMode=4&sortBy=&continuous_play_on=true&inPlaylist=1&stage_width=512&stage_height=296&user_id="+queueId;
		params.viewConstraints.addResolutionConstraint(512, 285); //16:9
 		}
 	else if (width/height < 16/9) {
		//params.uri = "chromeless_4-3.htm";
		params.uri = "http://www.hulu.com/site-player/playerembed.swf?referrer=none&eid="+embed+"&st=&et=&it=&siteHost=http://www.hulu.comhttp://www.hulu.com/embed/"+embed+"&popout=true&plugins=1&modes=0,4&initMode=4&sortBy=&continuous_play_on=true&inPlaylist=1&stage_width=512&stage_height=296&user_id="+queueId;
		params.viewConstraints.addResolutionConstraint(380, 285); //4:3
 		}
	params.audioCapture = true;
	params.htmlInputFocusMode = dcIPageDescriptor.FOCUS_MODE_OBJECT;
	// load the page
	var storage = connected.openStorage(embedStorage,true)
	storage.set(embedVideoId,embed) //contentId
	
	connected.debug.print("Playing: http://www.hulu.com/player.swf?eid="+videoId[4]+"&user_id="+queueId);
	connected.loadFromPageDescriptor(params);

}


function playbackPopupButtonHandler(e)
{
	var dctk = connected.toolkit;

	switch(e.buttonId)
	{
		case dcKeyEvent.BUTTON_ID_BACK:
		{
			dctk.dismissPopup();
		}
		break;
		
		case dcKeyEvent.BUTTON_ID_INFO:
		{
			dctk.dismissPopup();
			showInfoPopup();
		}
		break;
		
		case dcKeyEvent.BUTTON_ID_UP:
		{
			var descTxt = dctk.getElementById("descTxt");
			descTxt.scrollUp();
		}
		break;

		case dcKeyEvent.BUTTON_ID_DOWN:
		{
			var descTxt = dctk.getElementById("descTxt");
			descTxt.scrollDown();
		}
		break;

	}
}

function showPlaybackPopup_bak()
{
    var dctk = connected.toolkit;
    
    // create a new panel for the popup
    var popup = dctk.createPanelCtrl("popup", true);
    popup.width = 320;
    popup.height = 240;
    popup.outlined = true;
    popup.filled = true;
    popup.color.rgb = 0x000000;
    popup.color.a = 192;
    popup.onUnhandledKeyEvent = playbackPopupButtonHandler;
	
		var height = connected.outputHeight;
 	var width = connected.outputWidth;
	if (width/height >= 16/9) {
		//16:9
		titleFont = 24;
 		}
 	else 
		//4:3
		titleFont = 18;
    
    var popupTitleTxt = dctk.createLabelCtrl("popupTitleTxt", true);
    popupTitleTxt.setRect(20, 185, 300, 230);
    popupTitleTxt.textColor.rgb = 0xCDCDCD;
    popupTitleTxt.fontSize = titleFont;
	popupTitleTxt.align = dctkLabel.TS_CENTER;
	popupTitleTxt.valign = dctkLabel.TS_MIDDLE;
    popupTitleTxt.text = nowPlaying;
    popup.addChild(popupTitleTxt);
	
	var playThumb = dctk.createImageCtrl("playThumb", true);
	playThumb.setRect(0, 60, 320, 180);
	//playThumb.color.alpha = 0x33;
	playThumb.img.src = playDesc;
	popup.addChild(playThumb);
	
	var playBtn = dctk.createButtonCtrl("playBtn", true);
	playBtn.setRect(30, 10, 140, 40);
	playBtn.text = "Play";
	playBtn.fontSize = 24;
	playBtn.onSelect = function(e) { playMedia(); dctk.dismissPopup(); }
	popup.addChild(playBtn);
	
	var resumeBtn = dctk.createButtonCtrl("resumeBtn", true);
	resumeBtn.setRect(160, 10, 270, 40);
	resumeBtn.text = "Resume";
	resumeBtn.fontSize = 24;
	resumeBtn.onSelect = function(e) { resumeMedia(); dctk.dismissPopup(); }
	popup.addChild(resumeBtn);
	
	var popupDateTxt = dctk.createLabelCtrl("popupDateTxt", true);
    popupDateTxt.setRect(0, 45, 320, 55);
    popupDateTxt.textColor.rgb = 0xCDCDCD;
    popupDateTxt.fontSize = 14;
	popupDateTxt.align = dctkLabel.TS_CENTER;
	popupDateTxt.valign = dctkLabel.TS_MIDDLE;
    popupDateTxt.text = playDate;
    popup.addChild(popupDateTxt);
	    
    // show the popup
    dctk.invokePopup(popup);
	
	playBtn.rightSibling = resumeBtn;
	resumeBtn.leftSibling = playBtn;
	
	dctk.focusedElement = playBtn;
    }

function showPlaybackPopup()
{
    var dctk = connected.toolkit;
    
    // create a new panel for the popup
    var popup = dctk.createPanelCtrl("popup", true);
    popup.width = 600;
    popup.height = 440;
    popup.outlined = true;
    popup.filled = true;
    popup.color.rgb = 0x000000;
    popup.color.a = 192;
    popup.onUnhandledKeyEvent = playbackPopupButtonHandler;
	
	var height = connected.outputHeight;
 	var width = connected.outputWidth;
	if (width/height >= 16/9) {
		fontSize = 22; //16:9
		titleFont = 32;
 		}
 	else 
		fontSize = 17; //4:3
		titleFont = 28;
	
    
    var popupTitleTxt = dctk.createLabelCtrl("popupTitleTxt", true);
    popupTitleTxt.setRect(20, 385, 580, 425);
    popupTitleTxt.textColor.rgb = 0xCDCDCD;
    popupTitleTxt.fontSize = titleFont;
	popupTitleTxt.align = dctkLabel.TS_LEFT;
	popupTitleTxt.valign = dctkLabel.TS_MIDDLE;
    popupTitleTxt.text = main;
    popup.addChild(popupTitleTxt);
	
	var lengthLabel = dctk.createLabelCtrl("lengthLabel", true);
    lengthLabel.setRect(20, 360, 100, 380);
    lengthLabel.textColor.rgb = 0xCDCDCD;
    lengthLabel.fontSize = fontSize;
	lengthLabel.align = dctkLabel.TS_RIGHT;
	lengthLabel.valign = dctkLabel.TS_MIDDLE;
    lengthLabel.text = "Length: ";
    popup.addChild(lengthLabel);
	
	var lengthTxt = dctk.createLabelCtrl("lengthTxt", true);
    lengthTxt.setRect(100, 360, 345, 380);
    lengthTxt.textColor.rgb = 0xCDCDCD;
    lengthTxt.fontSize = fontSize;
	lengthTxt.align = dctkLabel.TS_LEFT;
	lengthTxt.valign = dctkLabel.TS_MIDDLE;
    lengthTxt.text = " ";
    popup.addChild(lengthTxt);
	
	var typeLabel = dctk.createLabelCtrl("typeLabel", true);
    typeLabel.setRect(20, 335, 100, 355);
    typeLabel.textColor.rgb = 0xCDCDCD;
    typeLabel.fontSize = fontSize;
	typeLabel.align = dctkLabel.TS_RIGHT;
	typeLabel.valign = dctkLabel.TS_MIDDLE;
    typeLabel.text = "Type: ";
    popup.addChild(typeLabel);
	
	var typeTxt = dctk.createLabelCtrl("typeTxt", true);
    typeTxt.setRect(100, 335, 345, 355);
    typeTxt.textColor.rgb = 0xCDCDCD;
    typeTxt.fontSize = fontSize;
	typeTxt.align = dctkLabel.TS_LEFT;
	typeTxt.valign = dctkLabel.TS_MIDDLE;
    typeTxt.text = " ";
    popup.addChild(typeTxt);
	
	var genreLabel = dctk.createLabelCtrl("genreLabel", true);
    genreLabel.setRect(20, 310, 100, 330);
    genreLabel.textColor.rgb = 0xCDCDCD;
    genreLabel.fontSize = fontSize;
	genreLabel.align = dctkLabel.TS_RIGHT;
	genreLabel.valign = dctkLabel.TS_MIDDLE;
    genreLabel.text = "Genre: ";
    popup.addChild(genreLabel);
	
	var genreTxt = dctk.createLabelCtrl("genreTxt", true);
    genreTxt.setRect(100, 310, 345, 330);
    genreTxt.textColor.rgb = 0xCDCDCD;
    genreTxt.fontSize = fontSize;
	genreTxt.align = dctkLabel.TS_LEFT;
	genreTxt.valign = dctkLabel.TS_MIDDLE;
    genreTxt.text = " ";
    popup.addChild(genreTxt);
	
	var networkLabel = dctk.createLabelCtrl("networkLabel", true);
    networkLabel.setRect(20, 285, 100, 305);
    networkLabel.textColor.rgb = 0xCDCDCD;
    networkLabel.fontSize = fontSize;
	networkLabel.align = dctkLabel.TS_RIGHT;
	networkLabel.valign = dctkLabel.TS_MIDDLE;
    networkLabel.text = "Company: ";
    popup.addChild(networkLabel);
	
	var networkTxt = dctk.createLabelCtrl("networkTxt", true);
    networkTxt.setRect(100, 285, 345, 305);
    networkTxt.textColor.rgb = 0xCDCDCD;
    networkTxt.fontSize = fontSize;
	networkTxt.align = dctkLabel.TS_LEFT;
	networkTxt.valign = dctkLabel.TS_MIDDLE;
    networkTxt.text = " ";
    popup.addChild(networkTxt);
	
	var releasedLabel = dctk.createLabelCtrl("releasedLabel", true);
    releasedLabel.setRect(20, 260, 100, 280);
    releasedLabel.textColor.rgb = 0xCDCDCD;
    releasedLabel.fontSize = fontSize;
	releasedLabel.align = dctkLabel.TS_RIGHT;
	releasedLabel.valign = dctkLabel.TS_MIDDLE;
    releasedLabel.text = "Released: ";
    popup.addChild(releasedLabel);
	
	var releasedTxt = dctk.createLabelCtrl("releasedTxt", true);
    releasedTxt.setRect(100, 260, 345, 280);
    releasedTxt.textColor.rgb = 0xCDCDCD;
    releasedTxt.fontSize = fontSize;
	releasedTxt.align = dctkLabel.TS_LEFT;
	releasedTxt.valign = dctkLabel.TS_MIDDLE;
    releasedTxt.text = " ";
    popup.addChild(releasedTxt);
	
	var expiresLabel = dctk.createLabelCtrl("expiresLabel", true);
    expiresLabel.setRect(20, 235, 100, 255);
    expiresLabel.textColor.rgb = 0xCDCDCD;
    expiresLabel.fontSize = fontSize;
	expiresLabel.align = dctkLabel.TS_RIGHT;
	expiresLabel.valign = dctkLabel.TS_MIDDLE;
    expiresLabel.text = "Expires: ";
    popup.addChild(expiresLabel);
	
	var expiresTxt = dctk.createLabelCtrl("expiresTxt", true);
    expiresTxt.setRect(100, 235, 345, 255);
    expiresTxt.textColor.rgb = 0xCDCDCD;
    expiresTxt.fontSize = fontSize;
	expiresTxt.align = dctkLabel.TS_LEFT;
	expiresTxt.valign = dctkLabel.TS_MIDDLE;
    expiresTxt.text = " ";
    popup.addChild(expiresTxt);
	
	var starsLabel = dctk.createLabelCtrl("starsLabel", true);
    starsLabel.setRect(20, 210, 100, 230);
    starsLabel.textColor.rgb = 0xCDCDCD;
    starsLabel.fontSize = fontSize;
	starsLabel.align = dctkLabel.TS_RIGHT;
	starsLabel.valign = dctkLabel.TS_MIDDLE;
    starsLabel.text = "Rating: ";
    popup.addChild(starsLabel);
	
	var stars = dctk.createImageCtrl("stars", true);
	stars.setRect(100, 230, 175, 208);
	stars.img.src = "0.png";
	popup.addChild(stars);
	
	var descLabel = dctk.createLabelCtrl("descLabel", true);
    descLabel.setRect(20, 185, 100, 205);
    descLabel.textColor.rgb = 0xCDCDCD;
    descLabel.fontSize = fontSize;
	descLabel.align = dctkLabel.TS_RIGHT;
	descLabel.valign = dctkLabel.TS_MIDDLE;
    descLabel.text = "Description: ";
    popup.addChild(descLabel);
	
	var descTxt = dctk.createLabelCtrl("descTxt", true);
    descTxt.setRect(60, 60, 325, 183);
    descTxt.textColor.rgb = 0xCDCDCD;
    descTxt.fontSize = fontSize;
	descTxt.align = dctkLabel.TS_LEFT;
	descTxt.valign = dctkLabel.TS_TOP;
	descTxt.multiline = true;
	descTxt.vertScroll = true;
    descTxt.text = "Loading... ";
    popup.addChild(descTxt);
	
	var playThumb = dctk.createImageCtrl("playThumb", true);
	playThumb.setRect(350, 50, 580, 380);
	//playThumb.color.alpha = 0x33;
	playThumb.img.src = playDesc;
	popup.addChild(playThumb);
	
	var playBtn = dctk.createButtonCtrl("playBtn", true);
	playBtn.setRect(20, 20, 130, 50);
	playBtn.text = "Play";
	playBtn.fontSize = 24;
	playBtn.onSelect = function(e) { playMedia(); dctk.dismissPopup(); }
	popup.addChild(playBtn);
	
	var resumeBtn = dctk.createButtonCtrl("resumeBtn", true);
	resumeBtn.setRect(140, 20, 250, 50);
	resumeBtn.text = "Resume";
	resumeBtn.fontSize = 24;
	resumeBtn.onSelect = function(e) { resumeMedia(); dctk.dismissPopup(); }
	popup.addChild(resumeBtn);
	
	var backBtn = dctk.createButtonCtrl("backBtn", true);
	backBtn.setRect(470, 20, 580, 50);
	backBtn.text = "Back";
	backBtn.fontSize = 24;
	backBtn.onSelect = function(e) { dctk.dismissPopup(); }
	popup.addChild(backBtn);
	
/*
	var popupDateTxt = dctk.createLabelCtrl("popupDateTxt", true);
    popupDateTxt.setRect(0, 45, 320, 55);
    popupDateTxt.textColor.rgb = 0xCDCDCD;
    popupDateTxt.fontSize = 14;
	popupDateTxt.align = dctkLabel.TS_CENTER;
	popupDateTxt.valign = dctkLabel.TS_MIDDLE;
    popupDateTxt.text = playDate;
    popup.addChild(popupDateTxt);
*/
	    
	startMetaRequest();	
    // show the popup
    dctk.invokePopup(popup);
	
	playBtn.rightSibling = resumeBtn;
	resumeBtn.leftSibling = playBtn;
	resumeBtn.rightSibling = backBtn;
	backBtn.leftSibling = resumeBtn;
	
	dctk.focusedElement = playBtn;
	//connected.toolkit.debug.drawElementOutline = true;
    }
